package com.wephone.Student.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.GroupListResponse;
import com.wephone.Student.bean.data.GroupInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;

public class GroupActivity extends BaseActivity {
	private ListView listView;
	private ListViewAdapter adapter;
	private List<GroupInfoData> results = new ArrayList<GroupInfoData>();
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(GroupActivity.this,GroupListResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					GroupListResponse res = (GroupListResponse)response;
					
					if(res.getData().size()<Constants.PAGE_SIZE){
						adapter.hasMore = false;
					}else{
						adapter.pageIdx++;
					}
					results.addAll(res.getData());
					adapter.notifyDataSetChanged();
				}
			}
		};
		return handler;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group);
		
		init();
	}
	
	private void init(){
		listView = (ListView)findViewById(R.id.listview);
		adapter = new ListViewAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new ListView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				GroupDetailActivity.groupInfo = results.get(arg2);
				startActivity(new Intent(getApplicationContext(),GroupDetailActivity.class));
			}});
		
		RequestParams params = new RequestParams();
		params.put("Page", "1");
		params.put("PageSize", ""+Constants.PAGE_SIZE);
		HttpClient.getInstance().get(Constants.API_GET_GROUP_LIST_URL, params, getHandler());
	}

	private class ListViewAdapter extends BaseAdapter{
		public int pageIdx = 1;
		public boolean hasMore = true;
		
		@Override
		public int getCount() {
			return results.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_group_list_item, null);
			}
			GroupInfoData data = results.get(position);
			TextView itemName = (TextView)convertView.findViewById(R.id.item_name);
			itemName.setText(data.getgTitle());
			TextView itemDescription = (TextView)convertView.findViewById(R.id.item_description);
			itemDescription.setText(data.getgDetail());
			TextView itemNum = (TextView)convertView.findViewById(R.id.item_num);
			itemNum.setText(data.getgMemberCount());
			
//			if(!TextUtils.isEmpty(data.get)){
//				ImageView itemImage = (ImageView)convertView.findViewById(R.id.item_image);
//				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+info.getuInfo().getuIcon(), itemImage);
//			}
			
			//load more
			if(position>=results.size()-1 && hasMore){
				RequestParams params = new RequestParams();
				params.put("Page", ""+pageIdx);
				params.put("PageSize", ""+Constants.PAGE_SIZE);
				HttpClient.getInstance().get(Constants.API_GET_GROUP_LIST_URL, params, getHandler());
			}
			return convertView;
		}		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			 new AlertDialog.Builder(this)
				.setTitle("提示")
				.setMessage("确定退出吗？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						finish();
					}
				}).show();
            return true; 
        }
        return super.onKeyDown(keyCode, event); 
	}
	
}
